package com.sca.bussiness.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author liuxianfei
 * @since 2020/11/20 11:52
 */
@Slf4j
@Configuration
public class RabbitConfig {

    public static final String QUEUE_POLL = "sca.schedule.poll";
    public static final String QUEUE_EXEC = "sca.schedule.exec";
    public static final String EXCHG_SCHEDULE = "sca.topic.schedule";
    public static final String RTKEY_WAIT_POLL = QUEUE_POLL;
    public static final String RTKEY_WAIT_EXEC = QUEUE_EXEC;

    private RabbitTemplate.ConfirmCallback confirmCallback = (correlationData, ack, cause) -> {
        if (!ack) {
            log.error("消息发送失败，[correlation：{}]，[cause：{}]", correlationData, cause);
        }
    };

    private RabbitTemplate.ReturnCallback returnCallback = (message, replyCode, replyText, exchange, routingKey) ->
        log.error("消息路由失败,[message:{}],[replyCode:{}],[replyText:{}],[exchange:{}],[routingKey:{}]", message, replyCode, replyText, exchange, routingKey);

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setConfirmCallback(confirmCallback);
        rabbitTemplate.setReturnCallback(returnCallback);
        return rabbitTemplate;
    }

}
